package com.guigusuqi.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.dto.DeleteList;
import com.guigusuqi.hy.dto.IntegralDetailDto;
import com.guigusuqi.hy.entity.IntegralDetail;
import com.guigusuqi.hy.mapper.IntegralDetailMapper;
import com.guigusuqi.hy.service.IntegralDetailService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 积分规则管理(IntegralDetail)表服务实现类
 *
 * @author makejava
 * @since 2022-04-04 18:30:57
 */
@Service("integralDetailService")
public class IntegralDetailServiceImpl extends ServiceImpl<IntegralDetailMapper, IntegralDetail> implements IntegralDetailService {

    /**
     * 分页获取所有积分详情,可以根据条件模糊查新
     * @param
     * @return
     */
    @Override
    public Result getIntegralDetail(Integer pageNum, Integer pageSize, IntegralDetailDto integralDetailDto)
    {
        LambdaQueryWrapper<IntegralDetail> wrapper = new LambdaQueryWrapper<>();
        if (integralDetailDto == null){
            wrapper = null;
        }else{
            if(integralDetailDto.getCount() != 0){
                wrapper.like(IntegralDetail::getCount,integralDetailDto.getCount());
            }
            if(integralDetailDto.getDetail() != null){
                wrapper.like(IntegralDetail::getDetail,integralDetailDto.getDetail());
            }
            if(integralDetailDto.getIntegralType() != null){
                wrapper.like(IntegralDetail::getIntegralType,integralDetailDto.getIntegralType());
            }
        }
        Page<IntegralDetail> page = new Page<>(pageNum, pageSize);
        page(page,wrapper);
        long total = page.getTotal();
        List<IntegralDetail> integralDetails = page.getRecords();
        return Result.success().data("integralDetails",integralDetails).data("total",total);
    }

    /**
     * 新增或者更新积分详情,如果前端传递的三个属性都一样，则无法添加
     * @param integralDetail
     * @return
     */
    @Override
    public Result addIntegralDetail(IntegralDetail integralDetail)
    {
        //判重
        LambdaQueryWrapper<IntegralDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IntegralDetail::getCount,integralDetail.getCount());
        wrapper.eq(IntegralDetail::getIntegralType,integralDetail.getIntegralType());
        wrapper.eq(IntegralDetail::getDetail,integralDetail.getDetail());
        List<IntegralDetail> list = list(wrapper);
        if(!list.isEmpty()){
            return Result.fail().message("已经存在相同数据,无需新增");
        }
        boolean save = saveOrUpdate(integralDetail);
        if (save){
            return Result.success().message("新增或者更新成功");
        }else {
            return Result.success().message("新增或者更新失败");
        }
    }

    /**
     * 批量删除
     * @param deleteList
     * @return
     */
    @Override
    public Result deleteIntegralDetail(DeleteList deleteList)
    {
        List<Long> ids = deleteList.getIds();
        boolean remove = removeByIds(ids);
//        for (Long id : ids) {
//            removeById(id);
//        }
        if (remove){
            return Result.success().message("删除成功");
        }else {
            return Result.success().message("删除失败");
        }
    }
}

